//  [9/4/2013 OMEGA] created

#ifndef _DUELASSEGROUPMODEL_H_
#define _DUELASSEGROUPMODEL_H_

#include <QAbstractItemModel>
#include "DuelAssECommon.h"

class DAssEGroupModelItem
{
public:
	DAssEGroupModelItem(DAssEGroupModelItem* parent = NULL);
	~DAssEGroupModelItem();
	enum 
	{
		GMIT_ROOT,
		GMIT_GROUP,
		GMIT_ATTACHED_ARCHIVE
	};

	DAssEGroupModelItem*	child(int row);
	int					childCount();
	// if a child does not exist, -1 will be returned.
	int					indexOf(DAssEGroupModelItem* child);
	bool				insertChildren(int position, int count);
	bool				removeChildren(int position, int count);
	QString		itemName;
	int			type;

	QList<DAssEGroupModelItem*>	childItems;
	DAssEGroupModelItem*			parentItem;
};

class DAssEGroupModel : public QAbstractItemModel
{
	Q_OBJECT
public:
	DAssEGroupModel(DAssEGroupView* parent);
	virtual ~DAssEGroupModel();

	void	reset(DAssEProject* proj);

	// add a group, return the newly added group's index 
	QModelIndex	addGroup(DAssEGroup* grp);
	void		removeGroup(DAssEGroup* grp);
	void		renameGroup(const QString& oldGrpName, const QString& newGrpName);

	// attach an archive to the group.
	void		attachArchiveToGroup(DAssEGroup* grp, DAssEArchive* arch);
	void		detachArchiveFromGroup(DAssEGroup* grp, DAssEArchive* arch);

	// if specified group does noe exist, then the root index will be returned.
	QModelIndex		getGroupIndex(const QString& grpName);

	// override : QAbstractItemModel
	virtual QModelIndex index( int row, int column, const QModelIndex &parent = QModelIndex( ) ) const;
	virtual QModelIndex parent( const QModelIndex &child ) const;
	virtual int rowCount( const QModelIndex &parent = QModelIndex( ) ) const;
	virtual int columnCount( const QModelIndex &parent = QModelIndex( ) ) const;
	virtual QVariant data( const QModelIndex &index, int role = Qt::DisplayRole ) const;
	virtual QVariant headerData(int section, Qt::Orientation orientation, int role) const;
	//////////////////////////////////////////////////////////////////////////
	Qt::ItemFlags flags(const QModelIndex &index) const;
	bool setData(const QModelIndex &index, const QVariant &value,
		int role = Qt::EditRole);
	bool insertRows(int position, int rows,
		const QModelIndex &parent = QModelIndex());
	bool removeRows(int position, int rows,
		const QModelIndex &parent = QModelIndex());
	//////////////////////////////////////////////////////////////////////////
	DAssEGroupModelItem*	getItem(const QModelIndex& index) const;

protected:
	// hacker for invalid internalPointer of qt.
	bool		exist(DAssEGroupModelItem* dstItm, DAssEGroupModelItem* parentItm) const;
	void		clearItems();

	DAssEGroupView*	mParent;

	DAssEGroupModelItem*	mRootItem;
};

#endif